import bugtracking.Bug

import bugtracking.TechnicalSupportIssuesDM
import bugtracking.technicalSupportIssues.OpenDate_Duration_TSI
import java.text.SimpleDateFormat
import org.apache.commons.lang.time.DateUtils

/**
 * Created by IntelliJ IDEA.
 * User: AlexandruA
 * Date: 7/5/11
 * Time: 10:54 AM
 * To change this template use File | Settings | File Templates.
 */
class DataMartCreationJob extends Observable {

    private List<Bug> bugList;

    DataMartCreationJob() {
//        addObserver();
    }

    static triggers = {
//        cron name: 'DataMartCreationJob', cronExpression: "00 49 15 ? * *"
          simple name: 'DataMartCreationJob', startDelay: 100
    }
    def group = "MyGroup"
//    def execute(){}

    def execute() {
        //store in datamart table bugs related to Technical Support Issues and are closed
        bugList = Bug.createCriteria().list {
            and {
                gt("delta_ts", new Date() - 10000);//TODO store update interval value in a config file, to set last update time and use that value here
            }
        }
        bugList.each {Bug bug ->
            TechnicalSupportIssuesDM technicalSupportIssuesDM = TechnicalSupportIssuesDM.findByBug_id(bug.bug_id);
            if (technicalSupportIssuesDM != null) {

                if (!bug.product.equals("Technical Support Issues")) {//the product has changed from Technical Support Issues to sth else
                    technicalSupportIssuesDM.delete();
                    OpenDate_Duration_TSI.findByBug_id(bug.bug_id).delete();
                } else {
                    if (bug.bug_status.equals("REOPENED")) {//only in case that after the bug was RESOLVED its states goes to REOPENED it is removed
                        technicalSupportIssuesDM.delete();
                        OpenDate_Duration_TSI.findByBug_id(bug.bug_id).delete();
                    } else {
                        technicalSupportIssuesDM.resolution = bug.resolution
                        technicalSupportIssuesDM.severity = bug.severity
                        technicalSupportIssuesDM.component = bug.component
                        technicalSupportIssuesDM.short_desc = bug.short_desc
                    }

                }
                //todo update tehnical support bug
            } else {
                if (bug.product.equals("Technical Support Issues")) {
                    if (bug.bug_status.equals("RESOLVED") && bug.resolution == "FIXED" ||
                            bug.bug_status.equals("CLOSED") && bug.resolution == "FIXED" ||
                            bug.bug_status.equals("VERIFIED") && bug.resolution == "FIXED") {//take all bug which are RESOLVED ->FIXED
                        def bugData = [bug_id: bug.bug_id,
                                creation_ts: bug.creation_ts,
                                bug_status: bug.bug_status,
                                resolution: bug.resolution,
                                severity: bug.severity,
                                product: bug.product,
                                component: bug.component,
                                short_desc: bug.short_desc
                        ]
                        def histories = bug.history.findAll {it.entries.find {it.addedValue == "FIXED"}}.toArray();
                        if (histories.length > 0) {
                            def closedDate = histories.last();
                            bugData.put("closed_date", closedDate.bug_when)
                            bugData.put("totalDuration", closedDate.bug_when - bug.creation_ts)
                        } else {
                            System.out.println();
                        }


                        println bugData
                        def temp = new TechnicalSupportIssuesDM(bugData);

                        def temp2 = new OpenDate_Duration_TSI(bug_id:bug.bug_id,creation_ts: DateUtils.truncate(bug.creation_ts, Calendar.DAY_OF_MONTH),"totalDuration":bugData.get("totalDuration"));
                        if(temp.validate()){
                            temp.save();
                        }
                        if(temp2.validate()){
                            temp2.save()
                        }
                    }
                }
            }
        }
        notifyObservers(bugList);
    }
}
